package jp.ac.waseda.cs.washi.game.entity;

import java.util.ArrayList;
import java.util.Arrays;

import jp.ac.waseda.cs.washi.game.MoveManager;
import jp.ac.waseda.cs.washi.game.api.MapInfo;
import jp.ac.waseda.cs.washi.gameaiarena.api.Direction4;
import jp.ac.waseda.cs.washi.gameaiarena.api.Point2;
import jp.ac.waseda.cs.washi.gameaiarena.common.PassPoint;
import jp.ac.waseda.cs.washi.gameaiarena.common.PassPointChooser;
import jp.ac.waseda.cs.washi.gameaiarena.common.PathSearcher;

public final class PathSearch {
	private PathSearch() { }

	public static Direction4[] Search(MapInfo map, Point2 from, Point2 to) {
		return Search(map, from, to, Direction4.values());
	}

	public static Direction4[] Search(final MapInfo map, Point2 from, Point2 to,
			final Direction4[] directions) {
		Direction4[] path = PathSearcher.getPath(from, to, Arrays.asList(directions), new PassPointChooser() {
			@Override
			public Iterable<PassPoint> getNextPassPoints(PassPoint lastPoint) {
				ArrayList<PassPoint> results = new ArrayList<PassPoint>();
				for (Direction4 direction : directions) {
					Point2 location = lastPoint.getLocation().move(direction);
					if (MoveManager.isMovable(map, location)) {
						results.add(new PassPoint(lastPoint, direction, 1));
					}
				}
				return results;
			}
		});
		return path;
	}
}
